//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.
**************************************************************************
*/

/**
* 导航插件类；
*/



define(function (require, exports, module) {
    var NavigationPanelMainFrmEventHandler = require('./WapNPMainFrmEventHandler.js');
	WebPDF.WapNavigationPanelPluginName = "WapNavigationPanelPlugin";
    /**
     * Plugin of navigation panel
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.NavigationPanelPlugin = function (readerApp) {
        //子窗口列表；需要实现INavigationChildPanel接口;
        var _childPanelList = [],
        //当前处于显示状态的Panel;
            _curVisibleChildPanel = null,
            _readerApp = readerApp,
            _appId = _readerApp.getAppId(),
            _gridArray = ['ui-grid-solo', 'ui-grid-a', 'ui-grid-b', 'ui-grid-c', 'ui-grid-d'],
            _blockArray = ['ui-block-a', 'ui-block-b', 'ui-block-c', 'ui-block-d', 'ui-block-e'],
            _self = this,
        //按钮面板ID
            _tabPanelId = _appId + '_tabPanel',
        //存放按钮的ul列表;
            //_buttonListId = _appId + '_btnList',
        //存放子面板内容;
            _treePanelId = _appId + '_fxtree_panel';
        //创建面板的div内容；
        var _panelSize =
        {
            treePanelWidth: 0,
            treePanelHeight: 0
        };
        function _createHtmlContent() {
            //顶端的按钮面板
            var classStr=_gridArray[0];
            if (_childPanelList.length > 0 && _childPanelList.length < 6) {
                classStr=_gridArray[_childPanelList.length - 1];
            }
            var html = '<div id="' + _tabPanelId + '"  class="ui-infopage-header ' + classStr + '" ></div>';

            //底部内容面板；
            html += '<div id="' + _treePanelId + '" class="fwr-left-panel-content" ' +
                'style="overflow: hidden; width: 100%;height: 100%;color:#456176;background-color:#f6f6f6;"></div>';
            //$("#infoPage").append(html);
            //$("#" + _readerApp.getMainView().getLeftPanelID()).append(html);
            var htmlPage='<div data-role="page" id="' + _appId  + '_infoPage">' +
                '<a href="#readerPage" data-transition="slide" data-role="button" data-icon="btnleft"' +
                ' data-iconpos="notext" data-corners="false" data-shadow="false"' +
                ' style="position:fixed;top:50%;margin-top:-2.875em;right:0;z-index:10000;"></a>' +
                html +
            '</div>';
            $("body").append(htmlPage);
        }

        function _bindEvents() {
            var childCount = _childPanelList.length;
            for (var i = 0; i < childCount; i++) {
                var panel = _childPanelList[i];
                $("#" + panel.getTabBtnID()).on('tap', { panel: panel }, function (event) {
                    _self.showChildPanel(event.data.panel);
                    return true;
                });
            }
            $(_readerApp).on(WebPDF.EventList.MAINFRAME_RESIZE,_onMainFrmSize);
        }

        /**
         * Add child panel
         * @param {WebPDF.INavigationChildPanel} childPanel child panel to add.
         */
        this.addChildPanel = function (childPanel) {
            if (!childPanel)
                return;
            _childPanelList.push(childPanel);
        };

        //显示某个子panel;
        this.showChildPanel = function (childPanelToShow) {
            if (!childPanelToShow){
                return;
            }
            if (_curVisibleChildPanel != null) {
                $("#" + _curVisibleChildPanel.getPanelID()).addClass("fwr-hidden");
                $("#" + _curVisibleChildPanel.getTabBtnID()).removeClass("ui-infopage-selected");
            }

            var jqPanel = $("#" + childPanelToShow.getPanelID());
            if (jqPanel.hasClass("fwr-hidden")) {
                jqPanel.removeClass("fwr-hidden");
                childPanelToShow.onSize(_panelSize.treePanelWidth, _panelSize.treePanelHeight);
                $("#" + childPanelToShow.getTabBtnID()).addClass("ui-infopage-selected");
            }
            _curVisibleChildPanel = childPanelToShow;
        };

        this.onSize = function () {
            _readerApp.onResize();
        };

        function _onMainFrmSize () {
            var jqTreePanel = $("#" + _treePanelId);
            var tabPanelHeight = $("#" + _tabPanelId).height();
            var jqLeftPanel =jqTreePanel.parent();
            var leftPanelHeight = jqLeftPanel.height() - tabPanelHeight;
            //这一句主要是为了兼容ie8，jqTreePanel高度设为100%获取的高度值确是错的;
            jqTreePanel.css({ top: tabPanelHeight, height: leftPanelHeight, width:jqLeftPanel.width()});
            var treePanelWidth = jqTreePanel.width();
            var treePanelHeight = jqTreePanel.height();
            _panelSize.treePanelWidth = treePanelWidth;
            _panelSize.treePanelHeight = treePanelHeight;
            if (_curVisibleChildPanel){
                _curVisibleChildPanel.onSize(treePanelWidth, treePanelHeight);
            }
        }

        //获取Plugin名称;
        this.getPluginName = function () {
            return WebPDF.WapNavigationPanelPluginName;
        };
        //插件被注册的时候调用;
        this.onRegister = function () {
        };
        //模块初始化;
        this.init = function () {
            _createHtmlContent();
            var childCount = _childPanelList.length;
            var jqTreePanel = $("#" + _treePanelId);
            var jqTabPanel = $("#" + _tabPanelId);
            for (var i = 0; i < childCount; i++) {
                var panel = _childPanelList[i];
                var styleStr = i==0 ? "": "border-left: 1px solid #c4c4c4;";
                jqTabPanel.append('<div class="' + _blockArray[i] + '" id="' + panel.getTabBtnID() +  '"><label style="margin:0;' + styleStr + '" data-i18n="' + panel.getTitle() + '"></label></div>');
                jqTreePanel.append("<div  id='" + panel.getPanelID() + "' class='fwr-hidden fwrJScrollPaneContainer fwrJScrollPaneScrollable '></div>");
            }

            _bindEvents();
            _readerApp.registerMainFrmEventHandler(new NavigationPanelMainFrmEventHandler(_self));
            _self.onSize();
            if (childCount > 0) {
                _self.showChildPanel(_childPanelList[0]);
            }
            $("#" + _tabPanelId).i18n();
            $("#" + _treePanelId).i18n();
        };

        //模块卸载;
        this.unload = function () {
        };
    };

    exports.createPlugin = function (readerApp) {
        return new WebPDF.NavigationPanelPlugin(readerApp);
    };
});